package com.ruoyi.web.controller.decision;

import com.alibaba.fastjson2.JSON;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.decision.domain.Scene;
import com.ruoyi.decision.domain.Task;
import com.ruoyi.decision.domain.Menu;
import com.ruoyi.decision.domain.UserTaskStrategy;
import com.ruoyi.decision.service.ISceneService;
import com.ruoyi.decision.service.ITaskService;
import com.ruoyi.decision.service.IUserTaskStrategyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;

/**
 * 预案Controller
 * 
 * @author zbw
 * @date 2022-07-19
 */
@RestController
@RequestMapping("/decision/task")
public class TaskController extends BaseController
{
    @Autowired
    private ITaskService taskService;
    @Autowired
    private ISceneService iSceneService;
    @Autowired
    private IUserTaskStrategyService userTaskStrategyService;


    /**
     * 查询决策场景列表-  树形菜单
     */
    @PreAuthorize("@ss.hasPermi('decision:task:slist')")
    @GetMapping("/slist")
    public TableDataInfo slist(Scene scene)
    {
        List<Menu> list = iSceneService.treemenu(scene);
        return getDataTable(list);
    }


    /**
     * 查询预案列表
     */
    @PreAuthorize("@ss.hasPermi('decision:task:list')")
    @PostMapping("/list")
    public TableDataInfo list(@RequestBody Task task)
    {
        startPage();
        List<Task> list = taskService.selectTaskList(task);
        if (!task.getUserId().equals("1")){
            List<UserTaskStrategy> userTaskStrategies = userTaskStrategyService.selectUserTaskStrategyList(new UserTaskStrategy(task.getUserId()));
            List<Task> objects = new ArrayList<>();
            HashSet<Task> tasks = new HashSet<>();
            for (UserTaskStrategy userTaskStrategy : userTaskStrategies) {
                for (Task task1 : list) {
                    if (task1.getTaskId().equals(userTaskStrategy.getTaskId())){
                        tasks.add(task1);
                    }
                }
            }
            for (Task task1 : tasks) {
                objects.add(task1);
            }
            return getDataTable(objects);
        }

        return getDataTable(list);
    }

    /**
     * 导出预案列表
     */
    @PreAuthorize("@ss.hasPermi('decision:task:export')")
    @Log(title = "预案", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Task task)
    {
        List<Task> list = taskService.selectTaskList(task);
        ExcelUtil<Task> util = new ExcelUtil<Task>(Task.class);
        util.exportExcel(response, list, "预案数据");
    }

    /**
     * 获取预案详细信息
     */
    @PreAuthorize("@ss.hasPermi('decision:task:query')")
    @GetMapping(value = "/{taskId}")
    public AjaxResult getInfo(@PathVariable("taskId") String taskId)
    {
        return AjaxResult.success(taskService.selectTaskByTaskId(taskId));
    }

    /**
     * 新增预案
     */
    @PreAuthorize("@ss.hasPermi('decision:task:add')")
    @Log(title = "预案", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Task task)
    {
        if (task.getTaskName()!=null && task.getSceneId()!=null && task.getProblemArea()!=null){
            Task task1 = taskService.insertTask(task);
            return AjaxResult.success(task1);
        }
        return AjaxResult.success(0);
    }

    /**
     * 修改预案
     */
    @PreAuthorize("@ss.hasPermi('decision:task:edit')")
    @Log(title = "预案", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Task task)
    {
        return toAjax(taskService.updateTask(task));
    }

    /**
     * 删除预案
     */
    @PreAuthorize("@ss.hasPermi('decision:task:remove')")
    @Log(title = "预案", businessType = BusinessType.DELETE)
	@DeleteMapping("/{taskIds}")
    public AjaxResult remove(@PathVariable String[] taskIds)
    {
        return toAjax(taskService.deleteTaskByTaskIds(taskIds));
    }
}
